# 使用不同節點可能會得到不同的搜索結果

import re
from get_net import GetNet

class GetWebSite1():
    def __init__(self, current_data:dict={}):
        self.main_url = "" # main url
        self.cookies = "" # cookies
        self.headers = {
            # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0",
            "User-Agent": "XD_beta",
            "cookie": self.cookies
            }
        self.current_data = current_data

    def identify(self,html) -> int:
        """傳入html 通過正則表達式獲取搜索結果數並返回 獲取失敗返回-1"""
        result = re.findall("Found ([0-9]*) results.", html)
        result = int(result[0]) if len(result) > 0 else -1
        return result

    def get_page(self, url: str) -> str:
        """請求鏈接 返回提示錯誤的文本 或者response.text"""
        downloader = GetNet(self.headers)
        response = downloader.get(url)
        response = response if isinstance(response, str) else response.text
        return response

    def get_result(self) -> dict:
        """獲取搜索數據"""
        new_data = {}
        for key in self.current_data:
            print("Getting from ws1 which keyword is " + key + ":")
            html = self.get_page(self.main_url.format(key))
            new_result = self.identify(html)
            new_data[key] = new_result
        return new_data
    
    def compare_data(self, new_data:dict) -> bool:
        """對比新舊數據 視情況補充"""
        pass

def test():
    ws1_data = {"test":0}
    downloader = GetWebSite1(ws1_data)
    print(downloader.get_result())

if __name__ == "__main__":
    test()